import { locations } from "../../../gpu/locations";
const LineGeometry =/*wgsl*/`
#include <VertexInput>
#include <VertexOutput>
#include <DisplacementMap_def>
#include <Camera>
#include <Animation>
#include <Trans_def>
#if ~{def.lineWidth}
@group(${locations.lineWidth[0]}) @binding(${locations.lineWidth[1]})   var<uniform> lineWidth:f32 ;
#endif
 fn getPosition(input:VertexInput)->vec4<f32>{
   var pos:vec4<f32> = vec4(input.position, 1.0);
  //  如果有变形动画
   #if ~{def.u_morphWeights}
    pos += morphPosition(input.VertexID);
   #endif
     //  如果有骨骼动画
   #if ~{def.u_jointsSampler}
    pos = fullMatrix() * pos;
   #endif
   #if ~{def.displacementMap && def.normal && def.texCood0}
    pos = vec4(pos.xyz+cal_displacementTrans(input),1.0);
   #endif 
    return pos;
  }
  fn pixelToWorld(pixel:f32)->f32{
    return (pixel/camera.viewport.y)*camera.dist*2.309401076758503;
  }
  #if ~{def.normal}
  fn getNormal(input:VertexInput)->vec3<f32>{
    var normal:vec3<f32> = input.normal;
    //  如果有变形动画
    #if ~{def.u_morphWeights}
     normal += morphNormal(input.VertexID);
    #endif
    //  如果有骨骼动画
    #if ~{def.u_jointsSampler}
    normal = mat3(skinNormalMatrix()) * normal;
    #endif
      return normalize(normal);
  }
  #endif
  #if ~{def.tangent&&def.normal}
  fn getTangent(input:VertexInput)->vec3<f32>{
    var  tangent:vec3<f32> = input.tangent.xyz;
    //  如果有变形动画
    #if ~{def.u_morphWeights}
      tangent += morphTangent(input.VertexID);
    #endif
    //  如果有骨骼动画
    #if ~{def.u_jointsSampler}
      tangent = mat3(fullMatrix()) * tangent;
    #endif
      return normalize(tangent);
  }
  #endif
@vertex
    fn main(vertexInput:VertexInput) -> VertexOutput {
      var output:VertexOutput;
      var modelMatrix:mat4x4<f32>;
      var u_NormalMatrix:mat4x4<f32>;
      // 从trans中分解处模型矩阵与法向量矩阵
      #if ~{def.trans}
      modelMatrix=trans.modelMat;
      u_NormalMatrix=trans.normalMat;
      #endif
      #if ~{def.transinstance}
      modelMatrix=trans[vertexInput.index].modelMat;
      u_NormalMatrix=trans[vertexInput.index].normalMat;
      #endif
      var pos= getPosition(vertexInput);
      #if ~{def.pointNormal}
      pos.x += vertexInput.pointNormal.x * 0.5 * pixelToWorld(lineWidth);
      pos.y += vertexInput.pointNormal.y * 0.5 * pixelToWorld(lineWidth);
      pos.z += vertexInput.pointNormal.z * 0.5 * pixelToWorld(lineWidth);
      #endif 
      pos= modelMatrix*pos;
      output.Position =camera.vp*pos;
      output.fragPosition=pos.xyz/pos.w;
      #if ~{def.normal}
      var  normal= getNormal(vertexInput);
      #else
      var normal= vec3(0,0,1);
      #endif 
        // 如果定义法向量与切线
      #if ~{def.normal&&def.tangent}
      var tangent:vec3<f32> = getTangent(vertexInput);
      var normalW:vec3<f32> = normalize((u_NormalMatrix * vec4(normal, 0.0)).xyz);
      var tangentW:vec3<f32> = normalize((modelMatrix * vertexInput.tangent).xyz);
      var bitangentW:vec3<f32> = cross(normalW, tangentW) * vertexInput.tangent.w;
      output.normalW=normalW;
      output.tangentW=tangentW;
      output.bitangentW=bitangentW;
      #else
      #if ~{def.normal}
      output.normal=normalize((u_NormalMatrix* vec4(normal, 1.0)).xyz);
      #endif 
      #endif 
      #if ~{def.texCood0}
      output.texCood0=vertexInput.texCood0;
      #endif 
      #if ~{def.texCood1}
      output.texCood1=vertexInput.texCood1;
      #endif 
      //  如果有变形动画
      #if ~{def.u_morphWeights&&def.texCood0}
      output.texCood0 += morphTexCoord0(vertexInput.VertexID);
      output.texCood1 += morphTexCoord1(vertexInput.VertexID);
      #endif 
      #if ~{def.color}
        #if ~{def.colorF32}
          #if ~{def.colorVec3}
            output.color=vec4<f32>(vertexInput.color.xyz,1.0)  ;
          #else 
             output.color=vec4<f32>(vertexInput.color);
          #endif 
       #else 
        #if ~{def.colorVec3}
          output.color=vec4<f32>(vertexInput.color.x/255,vertexInput.color.y/255,vertexInput.color.z/255,1.0)  ;
        #else 
          output.color=vec4<f32>(vertexInput.color/255);
         #endif 
       #endif 
        // #if ~{def.u_morphWeights}
        // output.color = clamp(output.color + morphColor0(vertexInput.VertexID).xyz, 0.0, 1.0);
        // #endif
      #endif 
      #if ~{def.uvWidthFactor}
      output.uvWidthFactor=vertexInput.uvWidthFactor;
      #endif 
      return output;
    }
`
export { LineGeometry }